<?php
/*********************************************************************************
** The contents of this file are subject to the vtiger CRM Public License Version 1.0
 * ("License"); You may not use this file except in compliance with the License
 * The Original Code is:  vtiger CRM Open Source
 * The Initial Developer of the Original Code is vtiger.
 * Portions created by vtiger are Copyright (C) vtiger.
 * All Rights Reserved.
*
 ********************************************************************************/

global $result;
global $client;

function checkFileAccess($filepath) {
	$root_directory = '';

	// Set the base directory to compare with
	$use_root_directory = $root_directory;
	if(empty($use_root_directory)) {
		$use_root_directory = realpath(dirname(__FILE__).'/../../.');
	}

	$realfilepath = realpath($filepath);

	/** Replace all \\ with \ first */
	$realfilepath = str_replace('\\\\', '\\', $realfilepath);
	$rootdirpath  = str_replace('\\\\', '\\', $use_root_directory);

	/** Replace all \ with / now */
	$realfilepath = str_replace('\\', '/', $realfilepath);
	$rootdirpath  = str_replace('\\', '/', $rootdirpath);

	if(stripos($realfilepath, $rootdirpath) !== 0) {
		die("Sorry! Attempt to access restricted file.");
	}
	return true;
}

function getblock_header($str,$headerspan='4',$ticketcloselink=false)
{
   if ($str == '') {
      $str = "&nbsp;";
      $class=" empty";
   }	
	if ($ticketcloselink == true) {
		$list .= '<tr>' . "\n" . '<th colspan="' . ($headerspan-1) . '" class="detailedViewHeader">' . getTranslatedString($str) . '</th>' . "\n";
		$list .= '<th class="detailedViewHeader">' . $ticketcloselink .'</th>' . "\n" . '</tr>' . "\n";
	} else {
		$list .= '<tr>' . "\n" . '<th colspan="' . ($headerspan) . '" class="detailedViewHeader' . $class . '">' . getTranslatedString($str) . '</th>' . "\n" . '</tr>' . "\n";
	}	
	return $list;

}

// Need to work out how to turn our block_array into an associative array.
// Not right now though...
function associate_array($block_array) {
   $arr = array();
   foreach ($block_array as $key => $value) {
      foreach ($value as $key1 => $value1) {
         echo "Key1: $key1 => Value1: $value1<br />";
      }
   }  
}


function getblock_fieldlist($block_array)
{  
   //associate_array($block_array);
	$list = '';
	$z = 0;
	$field_count = count($block_array);
	if($field_count != 0)
	{
		for($i=0;$i<$field_count;$i++,$z++)
		{
			$blockname = $block_array[$i]['blockname'];

			$data = $block_array[$i]['fieldvalue'];
			if($block_array[$i]['fieldlabel'] == 'Note'){
    			$data = html_entity_decode($data);
    		}
    		if($data =='')
				$data ='&nbsp;';
			if(strcmp($blockname,$block_array[$i-1]['blockname'])){
				if($blockname != 'Ticket Information') //hardcoded for tickets information block so that it'll not be displayed
					$list .= getblock_header($blockname);
				$z = 0;
			}
			if($z==0 || $z%2==0)
			$list .= '<tr>' . "\n";
			$list .= '<td class="dvtCellLabel">' . getTranslatedString($block_array[$i][fieldlabel]) . '</td>' . "\n";
			if(($z == 0 || $z%2 == 0) && ($i == ($field_count-1)))
			//if(($z == 0) && ($i > 0))
				$list .= '<td colspan="3" class="dvtCellInfo">' . $data . '</td>' . "\n";
			else
				$list .= '<td class="dvtCellInfo">' . $data . '</td>' . "\n";
			if($z%2 == 1 ||($i == ($field_count-1) ))
				$list .= '</tr>' . "\n";
		}	
	}
	return $list;
}

function getTranslatedString($str)
{
	global $app_strings;
	return (isset($app_strings[$str]))?$app_strings[$str]:$str;
}

// The function to get html format list data
// input array
// output htmlsource list array
// only for product
function getblock_fieldlistview_product($block_array,$module)
{
	
 $header = array();
 $header[0] = getTranslatedString($module);
 $header[1] = getTranslatedString('QUOTE_RELATED').getTranslatedString($module);	
 $header[2] = getTranslatedString('INVOICE_RELATED').getTranslatedString($module);	

 // All headers need the column-span of the top row. This fixes a bug where it was using
 // $nooffields below. This would be zero for empty rows casuing the header to get a colspan
 // of "0" too.
 $colspan = count($block_array[0][$module]['head'][0]);
 
 if($block_array == '')
 {
	$list .= '<tr><td class="error">' . $module . " " . getTranslatedString('LBL_NOT_AVAILABLE') . '</td></tr>' . "\n";
	return $list;
 }

for($k=0;$k<=2;$k++)
{

$header_arr = $block_array[$k][$module]['head'][0];	
$nooffields = count($header_arr);
$data_arr = $block_array[$k][$module]['data'];
	$noofdata = count($data_arr);
	$list .= getblock_header($header[$k],$colspan); // Note we use the top-row column count
	if($block_array[$k][$module]['data'] == ''){
	   $list .= '<tr><td class="notice">' . $header[$k] . " " . getTranslatedString('LBL_NOT_AVAILABLE') . '</td></tr>' . "\n";
	}
	if($nooffields!='')
	{
		
		$list .= '<tr class="detailedViewHeader">' . "\n";
		for($i=0;$i<$nooffields;$i++)
			$list .= '<td>' . getTranslatedString($header_arr[$i]['fielddata']) . '</td>' . "\n";
		$list .= '</tr>' . "\n";
	}	
	if($noofdata != '')
	{
		for($j=0;$j<$noofdata;$j++)
		{
			if($j==0||$j%2==0)
				$list .= '<tr class="dvtLabel">' . "\n";
			else
				$list .= '<tr class="dvtInfo">' . "\n";

			for($i=0;$i<$nooffields;$i++)
			{
				$data =$data_arr[$j][$i]['fielddata'];
				if($data == '')
					$data ='&nbsp;';
				$list .= '<td>' . $data . '</td>' . "\n";
			}
			$list .= '</tr>' . "\n";
		}
	}	
}
return $list;
}

// The function to get html format list data
// input array
// output htmlsource list array
//for quotes,notes and invoice

function getblock_fieldlistview($block_array,$block)
{
	if($block_array[0] == "#MODULE INACTIVE#"){
		$list .= '<tr><td class="error">' . getTranslatedString($block) . " " . getTranslatedString('MODULE_INACTIVE') . '</td></tr>';
		return $list;
	}
	if($block_array == ''){
		$list .= '<tr><td class="error">' . getTranslatedString($block) . " " . getTranslatedString('LBL_NOT_AVAILABLE') . '</td></tr>';
		return $list;
	}
	$header_arr = $block_array[0][$block]['head'][0];	
	$nooffields = count($header_arr);
	$data_arr = $block_array[1][$block]['data'];
	$noofdata = count($data_arr);
	if($nooffields!='') {
		$list .= '<tr>' . "\n";
		for($i=0;$i<$nooffields;$i++)
			$list .= "<th class='detailedViewHeader'>" . getTranslatedString($header_arr[$i]['fielddata']) . '</th>' . "\n";
		$list .= '</tr>' . "\n";
	}	
	if($noofdata != '') {
		for($j=0;$j<$noofdata;$j++) {
			if($j==0 || $j%2==0) {
				$list .='<tr class="dvtLabel">' . "\n";
			} else {
				$list .='<tr class="dvtInfo">' . "\n";
         }
			for($i=0;$i<$nooffields;$i++) {
				$data =$data_arr[$j][$i]['fielddata'];
				if($data == '') {
					$data = '&nbsp;';
			   }
				$list .= '<td>' . $data . '</td>' . "\n";
			}
			$list .= '</tr>' . "\n";
		}
   }
   return $list;
}

// The function to get html format url_list data
// input array
// output htmlsource list array
function getblockurl_fieldlistview($block_array,$block)
{
	$header_arr =$block_array[0][$block]['head'][0][0];	
	$nooffields=count($header_arr);
	$data_arr=$block_array[1][$block]['data'];
	$noofdata=count($data_arr);
	if($nooffields!='')
	{
		$list .='<tr class="detailedViewHeader">';
		for($i=0;$i<$nooffields;$i++)
			$list .= "<td>".getTranslatedString($header_arr[$i]['fielddata'])."</td>";
		$list .='</tr>';
	}	
	if($noofdata != '')
	{
		for($j=0;$j<$noofdata;$j++)
		{
			for($j1=0;$j1<count($data_arr[$j]);$j1++)
			{
				if($j1== '0'||$j1%2==0)
					$list .='<tr class="dvtLabel">';
				else
					$list .='<tr class="dvtInfo">';

				for($i=0;$i<$nooffields;$i++)
				{
					$data = $data_arr[$j][$j1][$i]['fielddata'];
					if($data =='')
						$data ='&nbsp;';
					if($i == '0' || $i== '1')
					{	if($j1 == '0')
						$list .= '<td rowspan="'.count($data_arr[$j]).'" >'.$data."</td>";
					}
					else
						$list .= "<td>".$data."</td>";
				}
				$list .='</tr>';
			}
		}
	}
   return $list;
}

/* 	Function to Show the languages in the Login page
*	Takes an array from PortalConfig.php file $language
*	Returns a list of available Language 	
*/
function getPortalLanguages() {
	global $languages,$default_language;
	foreach($languages as $name => $label) {
		if(strcmp($default_language,$name) == 0){
			$list .= '<option value=' . $name . ' selected="selected">' . $label . '</option>' . "\n";
		} else {
			$list .= '<option value=' . $name . '>' . $label . '</option>' . "\n";
		}
	}
	return $list;
}

/*	Function to set the Current Language
 * 	Sets the Session with the Current Language
 */
function setPortalCurrentLanguage() {
	global $default_language;
	if(isset($_REQUEST['login_language']) && $_REQUEST['login_language'] != ''){
		$_SESSION['portal_login_language'] = $_REQUEST['login_language'];
	} else {
		$_SESSION['portal_login_language'] = $default_language;
	}
}

/*	Function to get the Current Language
 * 	Returns the Current Language
 */
function getPortalCurrentLanguage() {
	global $default_language;
	if(isset($_SESSION['portal_login_language']) && $_SESSION['portal_login_language'] != ''){
		$default_language = $_SESSION['portal_login_language'];
	}
	return $default_language;
}

/** Function to  return a string with backslashes stripped off
 * @param $value -- value:: Type string
 * @returns $value -- value:: Type string array
 */
function stripslashes_checkstrings($value){
	if(is_string($value)){
		return stripslashes($value);
	}
	return $value;
}